// pages/mch/car/detail.js
const App = getApp()
import Kits from '../../../utils/kits'
import Util from '../../../utils/util'

Page({
    data: {
        init: false,

        car: {},
        forfeits: [],
        customer: {}
    },
    onLoad(options) {
        wx.sub('ReloadCarList', this, () => {
            this.execLoad(this.data.car.id)
        })
        this.execLoad(options.cid)
    },
    onUnload() {
        wx.cancel('ReloadCarList', this)
    },
    showTip() {
        wx.showToast({
            title: '该价格仅其他商家可见',
            icon: 'none'
        })
    },
    execLoad(cid) {
        App.doPost({
            uri: '/app/merchant/car/detail',
            body: {
                id: cid
            },
            fnSucc: resp => {
                resp.data.price.pdpStr = Kits.fmtMoneyString(resp.data.price.pdp)
                resp.data.price.pmpStr = Kits.fmtMoneyString(resp.data.price.pmp)
                resp.data.price.mdpStr = Kits.fmtMoneyString(resp.data.price.mdp)
                resp.data.price.depositStr = Kits.fmtMoneyString(resp.data.price.deposit)

                if(!resp.data.cover && Array.isArray(resp.data.photo) && resp.data.photo.length){
                    resp.data.cover = resp.data.photo[0]
                }
                this.setData({
                    init: true,
                    car: resp.data,
                })

                if(resp.data.order && resp.data.order.id) {
                    this.loadForfeits(resp.data.order.id)
                }
            }
        })
    },
    loadForfeits(id) {
        App.doPost({
            uri: '/app/merchant/order/detail',
            body: {
              id: id
            },
            fnSucc: resp => {
                if(resp.data.customer) {
                    this.setData({
                        customer: resp.data.customer
                    })
                }
            }
        })
        App.doPost({
            uri: '/app/merchant/violation/list',
            body: {
                order_id: Number(id)
            },
            fnSucc: resp => {
            const forfeits = resp.data.items
            if(Array.isArray(forfeits) && forfeits.length > 0) {
                forfeits.forEach(item => {
                item.DTime = this.formatTimestampMitute(item.Time)
                })
            }
            if(forfeits.length > 0 && this.data.order.bills) {
                const billsF = this.data.order.bills.filter(bill => !bill.paid)
                billsF.forEach((bill,index) => {
                    if(forfeits[index]){
                        forfeits[index].remark = bill.memo
                    }
                })
            }

            this.setData({
                forfeits: forfeits
            })
            }
        })
    },
    notFinish() {
        Util.Toast.fail('暂不可用')
    },
    toEdit() {
        if (this.data.car.audit.state == 2) {
            wx.navigateTo({
                url: `/pages/mch/car/edit?cid=${this.data.car.id}`,
            })
        } else {
            wx.navigateTo({
                url: `/pages/mch/car/update?cid=${this.data.car.id}`,
            })
        }

    },
    toStop() {
        wx.navigateTo({
            url: `/pages/mch/car/stop?cid=${this.data.car.id}`,
        })
    },
    toOrder() {
        wx.navigateTo({
            url: `/pages/mch/order/index?cid=${this.data.car.id}`,
        })
    },
    toCreate() {
        wx.navigateTo({
            url: `/pages/mch/order/create?cid=${this.data.car.id}`,
        })
    },
    doHide() {
        Util.Dialog.confirm(`是否下架车辆[${this.data.car.cno}]?`, () => {
            App.doPost({
                uri: '/app/merchant/car/show/set',
                body: {
                    id: this.data.car.id,
                    show: false
                },
                fnSucc: () => {
                    wx.pub("ReloadCarList", {})

                    this.execLoad(this.data.car.id)
                }
            })
        })
    },
    doShow() {
        Util.Dialog.confirm(`是否上架车辆[${this.data.car.cno}]?`, () => {
            App.doPost({
                uri: '/app/merchant/car/show/set',
                body: {
                    id: this.data.car.id,
                    show: true
                },
                fnSucc: () => {
                    wx.pub("ReloadCarList", {})

                    this.execLoad(this.data.car.id)
                }
            })
        })
    },

    toShare() {
        wx.navigateTo({
            url: '/pages/funcs/share',
            success: (res) => {
                res.eventChannel.emit('accept', {
                    type: 'car_share',
                    info: {
                        car: this.data.car,
                        mch: App.Global.User.merchant
                    }
                })
            }
        })
    },
    doPreviewItem(e) {
        let sources = []
        this.data.car.photo.forEach(p => {
          sources[sources.length] = {
            url: p,
            type: 'image'
          }
        })
    
        wx.previewMedia({
          sources: sources,
          current: 0
        })
      },
})